import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import NewsDetail from '../views/NewsDetail.vue'
import MyVant from '../views/MyVant.vue'
import UserView from '../views/UserView.vue' 
import LoginView from '../views/LoginView.vue'
import UserAdd from '../views/UserAdd.vue'
import store from '@/store/index.js'
import AddAddress from '@/views/AddAddress.vue'
import MyCenter from '@/views/MyCenter.vue'
import AddressList from '@/views/AddressList.vue'
import ShopView from '@/views/ShopView.vue'
import AboutView from '@/views/AboutView.vue'
import RegisterView from '@/views/RegisterView.vue'



Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    name: 'home',
    component: HomeView
  },
  {
    path: '/newsdetail/:id',
    name: 'newsdetail',
    component: NewsDetail
  },
  {
    path: '/myvant',
    name: 'myvant',
    component: MyVant
  },
  {
    path: '/userview',
    name: 'userview',
    component: UserView
  },
  {
    path: '/useradd',
    name: 'useradd',
    component: UserAdd
  },
  {
    path: '/',
    name: 'login',
    component: LoginView
  },
  {
    path: '/register',
    name: 'register',
    component: RegisterView
  },
  {
    path: '/aboutview/:id',
    name: 'aboutview',
    component: AboutView
  },
  {
    path: '/address',
    name: 'address',
    component: AddAddress
  },
  {
    path: '/center',
    name: 'center',
    component: () => import("../views/MyCenter.vue")
  },
  {
    path: '/list',
    name: 'list',
    component: AddressList
  },
  {
    path: '/shop',
    name: 'shop',
    component: () => import("../views/ShopView.vue")
  },
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  
  let isAuthenticated = localStorage.getItem('username')
  if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
  else next()
  })
export default router
